﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Configuration;
using System.Security.Cryptography;

using JigsawService.Parsers;
using JigsawService.DataAccess.Content;

namespace JigsawService.Process
{
    public class FileTransfer
    {
        /// <summary>
        /// All file transfers are based on a file block size of 1,000,000 bytes.
        /// This cannot be adjusted, due to some limitations in the implementation of blind (encrytped) data storage.
        /// </summary>
        public FileTransfer()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="localID"></param>
        /// <param name="blockNum"></param>
        /// <param name="checkSum"></param>
        /// <param name="fileBlock"></param>
        /// <returns></returns>
        public bool FileBlockUpload(long localID, int blockNum, string checkSum, byte[] fileBlock, long fileLength)
        {
            bool blockUpload = false;

            // verify checksum of fileblock compared to input checksum


            // verify that local filepath and file exists


            // create fileinfo instance for file


            // verify initial length of file compared to blocknum


            // append file block to file


            // read block from file and compare results to checksum (read after write)


            // if fileBlock length is less than 1,000,000 bytes it was the last one: do an extra test to compare total file length to fileLength value


            return blockUpload;
        }


        public byte[] FileBlockDownload(string fileGID, int blockNum)
        {
            byte[] downloadBlock = new byte[0];
            long offset = 0;

            if (blockNum >= 0)
            {
                if (blockNum > 0) offset = blockNum * 1000000;
            
                // get file info of currently active file version
                FileDA fda = new FileDA();
            }

            return downloadBlock;
        }

        /// <summary>
        /// distribute files in the repository so that no more than 1000 files are stored in any one folder
        /// (based on the LocalID value of each metadata record, so file paths will differ from one site to another)
        /// </summary>
        /// <param name="localID"></param>
        /// <returns></returns>
        public string BuildFilePath(long localID)
        {
            string localPath = "";
            string reposPath = ConfigurationManager.AppSettings["ReposPath"].ToString();



            return localPath;
        }


        /// <summary>
        /// returns a string representation of the MD5 hash value of a file block,
        /// verifies that file block contents were uploaded correctly and written correctly
        /// </summary>
        /// <param name="localPath"></param>
        /// <returns></returns>
        public string GetFileHash(byte[] fileBlock)
        {
            string hashVal = "";

            if (fileBlock != null && fileBlock.Length > 0)
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] hash = md5.ComputeHash(fileBlock);
                hashVal = BitConverter.ToString(hash);
            }

            return hashVal;
        }
    }
}